package com.itheima.web.controller.system;

import com.itheima.domain.system.User;
import com.itheima.utils.BeanUtil;
import com.itheima.utils.MD5Util;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Set;

@WebServlet("/system/user")
public class UserServlet extends BaseServlet {
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String verifycode = request.getParameter("verifycode");
        HttpSession session = request.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        request.removeAttribute("CHECKCODE_SERVER");
        if (!verifycode.equalsIgnoreCase(checkcode_server)){
            request.setAttribute("msg","验证码错误!");
            request.getRequestDispatcher("/pages/login.jsp").forward(request, response);
            return;
        }

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userService.login(username, password);

        if (user != null) {
            request.getSession().setAttribute("username",user.getUsername());
            request.getSession().setAttribute("user", user);
            String autoLogin  = request.getParameter("ck");
            if (autoLogin !=null){
                Cookie cookie = new Cookie("autoLoginCookie", URLEncoder.encode(username, "UTF-8")+"@"+password);
                cookie.setMaxAge(10 * 24 * 60 * 60 ); //10天
                cookie.setPath("/");
                response.addCookie(cookie);
            }
            request.getRequestDispatcher("/pages/main.jsp").forward(request, response);
        } else {
            request.setAttribute("name",username);
            request.setAttribute("msg", "登录失败,用户名或密码错误!!!");
            request.getRequestDispatcher("/pages/login.jsp").forward(request, response);
        }
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute("username");
        Cookie cookie = new Cookie("autoLoginCookie", "");
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
        request.getRequestDispatcher("/pages/login.jsp").forward(request, response);

    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        request.setAttribute("user", user);
        request.getRequestDispatcher("/pages/user-save.jsp").forward(request, response);
    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String password = request.getParameter("password");
        if (password!=""){
            User user =userService.findById(id);
            user.setPassword(password);
            userService.update(user);
            response.sendRedirect(request.getContextPath()+"/system/user?operation=logout");
        }else {
            request.getRequestDispatcher("/pages/user-save.jsp").forward(request, response);
        }

    }

    public void forget (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String email = request.getParameter("email");
        String phoneNum = request.getParameter("phoneNum");
        String password = request.getParameter("password");
        User user = userService.forget(email, phoneNum);
        if (user!=null){
            user.setPassword(password);
            userService.update(user);
            request.getRequestDispatcher("/pages/jump.html").forward(request, response);
        }else {
            request.setAttribute("email", email);
            request.setAttribute("phoneNum", phoneNum);
            request.setAttribute("password", password);
            request.setAttribute("msg","邮箱或手机号错误!!!");
            request.getRequestDispatcher("/pages/user-back.jsp").forward(request, response);
        }
    }
}
